﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
using Utility;
using Message = Utility.Message;

namespace _411Client.Connect
{
    public class Comunicate
    {
        /// <summary>
        /// 套接字对象
        /// </summary>
        public Socket S { get; set; }
        //信息发送存储
        private readonly Connect _con;
        private MyMessage _message;
        public Comunicate(Socket s, Connect con)
        {
            S = s;
            _con = con;

        }

        //与服务器进行通信连接
        public void Run()
        {
            try
            {
                while (true)
                {
                    byte[] recvBytes = new byte[1024000];
                    S.Receive(recvBytes, recvBytes.Length, 0); //从服务器端接受信息
                    _message = (MyMessage)Serialize.Userialize(recvBytes);
                    switch (_message.MessageType)
                    {
                        case MessageType.MESSAGE_MESS:
                            Message message = _message.Mymessage;
                            Program.Win.ReceiveMessage(message);
                            break;
                        case MessageType.MESSAGE_USER:
                            
                            _con.MyFriend = _message.Accepter;
                            Program.Win.GetFriend();
                            break;

                        case MessageType.MESSAGE_IMG:
                            Program.Win.ReceiveImage(_message.Myimage);
                            break;

                        case MessageType.MESSAGE_FILE:
                            _con.ReceiveFileConnect(_message.Ip, _message.Port, _message.Name);
                            _con.Filepath = _message.Name;
                            break;
                        case MessageType.MESSAGE_OK:
                            Thread thread = new Thread(Program.Win.PermitFile) {IsBackground = true};
                            thread.Start();
                            break;
                        case MessageType.MESSAGE_CANCEL:
                            Program.Win.CancelFile();
                            break;
                        case MessageType.MESSAGE_REJECT:
                            Program.Win.RejectFile();
                            break;
                        case MessageType.MESSAGE_CLOSE:
                            if (_con.Filerequest)
                            {
                                _con.Filerequest = false;
                                Program.Win.CancelFile();
                            }
                            Program.Win.Disconnect();
                            break;

                    }
                }

            }
            catch (Exception)
            {
                S.Close();
            }

        }

    }
}
